<template>
    <BaseDialog :title="(!props.item.categoryId ? '新增' : '编辑') + '分类'" :handleCancel="handleCancel"
        :dialogVisible="props.dialogVisible">
        <BaseForm type="rule" :handleOk="handleOk" :handleCancel="handleCancel" :items="formItems" v-model:form="ruleForm"
            label-width="100px" :rules="rules" :refresh="handleSearch">
        </BaseForm>
    </BaseDialog>
</template>
  
<script setup lang="ts">
import BaseForm from '@/components/base-form.vue'
import BaseDialog from '@/components/base-dialog.vue'
import { libSensitiveService } from '@/api/libSensitive'
import { libraryService } from '@/api/library'
const props = defineProps({
    dialogVisible: {
        type: Boolean,
    },
    item: {
        type: Object,
    },
    handleSearch: {
        type: Function,
    },
})
const emit = defineEmits(['update:dialogVisible'])

const formItems = ref([
    {
        name: '分类名称',
        key: 'categoryName',
        type: 'input',
    },
    {
        name: '舆情等级',
        key: 'level',
        type: 'select',
        options: [{ label: '高级', value: 1 }, { label: '普通', value: 2 }]
    },
    {
        name: '排序数字',
        key: 'sort',
        type: 'number',
        placeholder: '请输入排序数字，数字越小越靠前',
        min: 0,
    },
])
const rules = reactive({
    categoryName: [{ required: true, message: '请输入', trigger: 'change' }, { max: 255, message: '最长255个字符' }],
    level: [{ required: true, message: '请选择', trigger: 'change' }],
})
const ruleForm = ref({
   
    sort: ref<Number>(),
})
const handleOk = (data) => {
    if (props.item.categoryId) {
        return libSensitiveService.update({
            data: { ...ruleForm.value },
        })
    } else {
        return libSensitiveService.add({
            data: {
                ...ruleForm.value,
                //dictionaryValue: ruleForm.value.dictionaryKey,
            },
        })
    }
    

}
const handleCancel = () => {
    emit('update:dialogVisible', false)
}
onMounted(() => {
    if (props.item.categoryId) {
        ruleForm.value.categoryId = props.item.categoryId
        ruleForm.value.categoryName = props.item.categoryName
        ruleForm.value.level = props.item.level
        ruleForm.value.sort = props.item.sort
    }

})
</script>
  
<style lang="scss" scoped></style>
  